#ifndef _BUTTON_H
#define _BUTTON_H

#include "GraphicElement.h"

class Button : public GraphicElement
{
private:
	int control_id;
	Bitmap *buttonBg;
	Color *borderColor;

	bool isClicked;

public:

	Button(int x, int y, int width, int height, HWND hWnd, int control_id, WCHAR imgPath[], int imgX, int imgY): GraphicElement(x, y, width, height, hWnd)
	{
		this->control_id = control_id;
		borderColor = &BUTTON_BORDER_COLOR;
		isClicked = false;

		// Randa exe kelia
		WCHAR buffer[MAX_PATH];
		GetModuleFileNameW(NULL, buffer, sizeof(buffer));
		wstring exeDir = buffer;
		int place = exeDir.find_last_of('\\');
		exeDir = exeDir.substr(0, place);
		_wchdir(exeDir.c_str());

		//wstring imPat = imgPath;
		int counter = 0;
		for(int i = place+1; i < MAX_PATH; i++)
		{
			buffer[i] = imgPath[counter];
			if(imgPath[counter] == '\0')
			{
				break;
			}
			counter++;
		}

		buttonBg = new Bitmap(width, height);
		Bitmap *b = new Bitmap(buffer); //L"img\\bitmap.bmp"
		Graphics *fromImgButtonBg = Graphics::FromImage(buttonBg);
		fromImgButtonBg->DrawImage(b, 0, 0, imgX, imgY, width, height, UnitPixel);

		delete b;
		delete fromImgButtonBg;
	}

	~Button()
	{
		delete buttonBg;
	}


	void Draw(HDC hdc)
	{
		if(redraw)
		{
			Bitmap bmp(dimensions->width,dimensions->height);

			Graphics *fromImg = Graphics::FromImage(&bmp); //

			

			Pen			penBorder(*borderColor);

			// nupiesiamas fonas
			//fromImg->FillRectangle(&brushBackground, 1, 1, width-2, height-2);
			// remelis
			

			fromImg->DrawImage(buttonBg, 0, 0, dimensions->width, dimensions->height);
			fromImg->DrawRectangle(&penBorder, 0, 0, dimensions->width-1, dimensions->height-1);
	
			Graphics    graphics(hdc);
			graphics.DrawImage(&bmp, dimensions->x, dimensions->y, dimensions->width, dimensions->height);

			delete fromImg;
			redraw = false;
		}
	}


	void MouseDown()
	{
		if(checkBoundingBox())
		{
			isClicked = true;
			SendMessage(hWnd, WM_COMMAND, control_id, 0);
			borderColor = &BUTTON_BORDER_COLOR_MOUSE_DOWN;
			UpdateGraphics();
		}

	}

	void MouseUp()
	{
		if(isClicked)
		{
			isClicked = false;
			borderColor = &BUTTON_BORDER_COLOR;
			UpdateGraphics();
		}
	}

	void Hover()
	{
		if(checkBoundingBox())
		{
			if(isClicked == false)
			{
				isClicked = true;
				borderColor = &BUTTON_BORDER_COLOR_HOVER;
				UpdateGraphics();
			}
		}
		else
		{
			if(isClicked)
			{
				isClicked = false;
				borderColor = &BUTTON_BORDER_COLOR;
				UpdateGraphics();
			}
		}
	}

};


#endif